package org.zh.sh.util;

import com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class StaticPub {

    public static final String basePath = "http://localhost:9000";
    public static final String contentType = "Content-Type";
    public static final String APPLICATION_JSON_VALUE = "application/json";
    public static final String acceptCharset = "Accept-Charset";
    public static final HttpClient httpClient_1_1 = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1)
            .followRedirects(HttpClient.Redirect.NORMAL).connectTimeout(Duration.ofSeconds(20)).build();


    public static HttpResponse<String> smartPost(String path, Object body) {
        String jsonString = JSON.toJSONString(body);
        HttpRequest request = HttpRequest.newBuilder()
                .header(contentType, APPLICATION_JSON_VALUE)
                .header(acceptCharset, "utf-8")
                .POST(HttpRequest.BodyPublishers.ofString(jsonString))
                .uri(URI.create(basePath + path))
                .build();
        try {
            return httpClient_1_1.send(request, HttpResponse.BodyHandlers.ofString());
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }




}
